package cn.edu.swu.auth;

import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;

public class AuthCodeServlet extends HttpServlet {

    public final static String AUTH_CODE = "AUTH_CODE";

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String code = this.getRandomCode(4);
        BufferedImage image = this.createImage(90, 30, code, 10);
        HttpSession session = request.getSession(true);
        session.setAttribute(AUTH_CODE, code);

        response.setContentType("image/jpg");
        //write只能用来写文本,OutputStream是二进制文件
        try(OutputStream outputStream = response.getOutputStream()){
            ImageIO.write(image, "jpg", outputStream);
        }
    }

//    private void imageTest() throws IOException {
//        String code = this.getRandomCode(4);
//        BufferedImage image = this.createImage(120, 40, code, 10);
//        File outputfile = new File("E:/java/image.jpg");     //画完的图片保存的地方
//        ImageIO.write(image, "jpg", outputfile);                  //保存的格式
//    }

    private BufferedImage createImage(int width, int height, String code, int lines) {
        // 创建空白图片,RGBA表示透明图片
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        // 获取图片画笔
        Graphics2D graphic = image.createGraphics();
        // 设置抗锯齿，设置渲染细节
        graphic.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        // 绘制矩形背景
        graphic.setBackground(Color.WHITE);
        graphic.fillRect(0, 0, width, height);

        // 计算每个字符占的宽度，这里预留一个字符的位置用于左右边距
        int codeWidth = width / (code.length() + 1);
        // 字符所处的y轴的坐标
        int y = height * 3 / 4;

        for (int i = 0; i < code.length(); i++) {
            // 设置随机颜色
            graphic.setColor(getRandomColor());
            // 初始化字体
            Font font = new Font(null, Font.BOLD + Font.ITALIC, 35);
            graphic.setFont(font);      //加到画笔里面
            // 计算当前字符绘制的X轴坐标
            int x = (i * codeWidth) + (codeWidth / 2);
            // 画字符
            graphic.drawString(String.valueOf(code.charAt(i)), x, y);
        }
        // 画干扰线
        Random ran = new Random();
        for (int i = 0; i < lines; i++) {
            // 设置随机颜色
            graphic.setColor(getRandomColor());
            // 随机画线
            graphic.drawLine(ran.nextInt(width), ran.nextInt(height), ran.nextInt(width), ran.nextInt(height));
        }

        return image;
    }

    //获得随机元素
    private Color getRandomColor() {
        Random ran = new Random();
        Color color = new Color(ran.nextInt(256), ran.nextInt(256), ran.nextInt(256));
        return color;
    }

    //产生随机编码
    static String CHARS = "abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    private String getRandomCode(int count) {
        StringBuilder sb = new StringBuilder();
        Random random = new Random();
        for (int i=0; i<count; i++) {
            sb.append(CHARS.charAt(random.nextInt(CHARS.length()-1)));
        }
        return sb.toString();
    }
}
